$(function(){
	require.config({
        paths: {
            echarts: 'js/echarts'
        }
    });
	
	var word = decodeURI(GetQueryString('word'));
	
	//国家报道数据分布图
	requestSearchStations(word);
	
	//媒体报道数据分布图
	requestSearchMedia(word);
	
	//媒体资讯走势图
	requestSearchTrend(word);
	
	//媒体类型统计图
	requestSearchType(word);
	
	//多语言对比分析
	requestSearchLanguage(word);
	
	//正负面
	//requestSide("32");
});

function GetQueryString(name){
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
}


function hmProxy(url, data, callback){
	var prefix = localStorage.localIP;
	//if(!url.startsWith('/')){
	//	prefix += '/';
	//}
	$.ajax({
		type: 'post',
		url: prefix + url + '.json?message=' + JSON.stringify(data),
		dataType: 'json',
		success: callback
	});
}


function requestSearchStations(word){
	var json = {
		"word":word,
		"starttime":"",
		"terminaltime":"",
		"language":""
	};
	hmProxy('search/searchStations', json, function(data, textStatus, jqXHR){
		data = eval("("+data+")");
//		//测试数据
//		data = {
//			"ret": 0,
//			"msg": "success",
//			"data": [{
//				"nation": "香港",
//				"quantity": "18820"
//			}, {
//				"nation": "新加坡",
//				"quantity":"15975"
//			}, {
//				"nation": "英国",
//				"quantity":"13638"
//			}, {
//				"nation": "台湾",
//				"quantity":"8957"
//			}]
//		};
		
		if(data && data.ret == 0){
			var list = data.data;
			if(list && list.length > 0){
				var legendArr = new Array();
				var seriesArr = new Array();
				for(var i = 0; i < 8; i++){
					var obj = list[i];
					legendArr.push(obj.nation);
					var o = new Object();
					o.name = obj.nation;
					o.type = 'bar';
					o.data = [obj.quantity];
					seriesArr.push(o);
				};
				
				require(
			         [
			             'echarts',
			             'echarts/chart/bar'
			         ],
			         function (ec) {
			        	 var myChart = ec.init(document.getElementById('pic1'));
			        	 option = {
	        			    title : {
	        			        text: '',
	        			        subtext: ''
	        			    },
	        			    tooltip : {
	        			        trigger: 'axis'
	        			    },
	        			    legend: {
	        			        data:legendArr
	        			    },
	        			    calculable : true,
	        			    xAxis : [
	        			        {
	        			            type : 'category',
	        			            data : ['']
	        			        }
	        			    ],
	        			    yAxis : [
	        			        {
	        			            type : 'value'
	        			        }
	        			    ],
	        			    series : seriesArr
	        			};
			        	 myChart.setOption(option);
			         }
				);
			}
			
		}
	});
};

function requestSearchMedia(word){
	var json = {
			"word":word,
			"starttime":"",
			"terminaltime":"",
			"language":""
		};
	
	hmProxy('search/searchMedia', json, function(data, textStatus, jqXHR){
		data = eval("("+data+")");
		//测试数据
//		data = {
//			"ret": 0,
//			"msg": "success",
//			"data": [{
//				"media": "香港媒体",
//				"quantity": "17654"
//			}, {
//				"media": "新加坡媒体",
//				"quantity":"16332"
//			}, {
//				"media": "英国媒体",
//				"quantity":"16100"
//			}, {
//				"media": "台湾媒体",
//				"quantity":"15233"
//			}]
//		};
		if(data && data.ret == 0){
			var list = data.data;
			if(list && list.length > 0){
				var legendArr = new Array();
				var seriesArr = new Array();
				for(var i = 0; i < list.length; i++){
					var obj = list[i];
					if(i < 6){
						legendArr.push(obj.media);
						var o = new Object();
						o.name = obj.media;
						o.type = 'bar';
						o.data = [obj.quantity];
						seriesArr.push(o);
					}
				};
				require(
			         [
			             'echarts',
			             'echarts/chart/bar'
			         ],
			         function (ec) {
			        	 var myChart = ec.init(document.getElementById('pic2'));
			        	 option = {
	        			    title : {
	        			        text: '',
	        			        subtext: ''
	        			    },
	        			    tooltip : {
	        			        trigger: 'axis'
	        			    },
	        			    legend: {
	        			        data:legendArr
	        			    },
	        			    calculable : true,
	        			    xAxis : [
	        			        {
	        			            type : 'category',
	        			            data : ['']
	        			        }
	        			    ],
	        			    yAxis : [
	        			        {
	        			            type : 'value'
	        			        }
	        			    ],
	        			    series : seriesArr
	        			};
			        	 myChart.setOption(option);
			         }
				);
			}
		}
	});
};


function requestSearchTrend(word){
	var json = {
		"word":word,
		"starttime":"",
		"terminaltime":"",
		"language":""
	};
	
	hmProxy('search/searchTrend', json, function(data, textStatus, jqXHR){
		data = eval("("+data+")");
		//测试数据
//		data = {
//			"ret": 0,
//			"msg": "success",
//			"data": [{
//				"day": "11-14",
//				"quantity": "900"
//			}, {
//				"day": "11-15",
//				"quantity":"1200"
//			}, {
//				"day": "11-16",
//				"quantity":"210"
//			}, {
//				"day": "11-17",
//				"quantity":"334"
//			}, {
//				"day": "11-18",
//				"quantity":"2590"
//			}, {
//				"day": "11-19",
//				"quantity":"1230"
//			}, {
//				"day": "11-20",
//				"quantity":"3210"
//			}]
//		};

		if(data && data.ret == 0){
			var list = data.data;
			if(list && list.length > 0){
				var xAxisArr = new Array();
				var seriesArr = new Array();
				list.forEach(function(obj){
					xAxisArr.push(obj.day);
					seriesArr.push(obj.quantity);
				});
				
				require(
			         [
			             'echarts',
			             'echarts/chart/line'
			         ],
			         function (ec) {
			        	 var myChart = ec.init(document.getElementById('pic3'));
			        	 option = {
	        			    tooltip : {
	        			        trigger: 'axis'
	        			    },
	        			    legend: {
	        			        data:['七天内检测总量走势图']
	        			    },
	        			    calculable : true,
	        			    xAxis : [
	        			        {
	        			            type : 'category',
	        			            boundaryGap : false,
	        			            data : xAxisArr
	        			        }
	        			    ],
	        			    yAxis : [
	        			        {
	        			            type : 'value'
	        			        }
	        			    ],
	        			    series : [
	        			        {
	        			            name:'检测总量',
	        			            type:'line',
	        			            stack: '总量',
	        			            data: seriesArr
	        			        }
	        			    ]
	        			};
			        	 myChart.setOption(option);
			         }
			    );
			}
		}
	});
};

function requestSearchType(word){
	var json = {
		"word":word,
		"starttime":"",
		"terminaltime":"",
		"language":""
	};

	hmProxy('search/searchType', json, function(data, textStatus, jqXHR){
		data = eval("("+data+")");
		//测试数据，这个需要看红麦返回值rate是什么格式的，分三种:
		//1、小数；2：个数；3：百分比
//		data = {
//			"ret": 0,
//			"msg": "success",
//			"data": [{
//				"type": "博客",
//				//"rate": "0.1"
//				"rate": "115"
//				//"rate": "13%"
//			}, {
//				"type": "平媒",
//				//"rate":"0.25"
//				"rate": "125"
//				//"rate": "27%"
//			}, {
//				"type": "论坛",
//				//"rate":"0.3"
//				"rate": "75"
//				//"rate": "25%"
//			}, {
//				"type": "新闻",
//				//"rate":"0.35"
//				"rate": "155"
//				//"rate": "35%"
//			}]
//		};
		if(data && data.ret == 0){
			var list = data.data;
			if(list && list.length > 0){
				var legendArr = new Array();
				var seriesArr = new Array();
				list.forEach(function(obj){
					legendArr.push(obj.type);
					var o = new Object();
					o.name = obj.type;
					//小数的话 * 100
					//o.value = obj.rate * 100;
					//个数的话，直接复制
					o.value = obj.rate;
					//o.value = obj.rate.substr(0, obj.rate.length-1);
					seriesArr.push(o);
				});
				
				require(
			         [
			             'echarts',
			             'echarts/chart/pie'
			         ],
			         function (ec) {
			        	 var myChart = ec.init(document.getElementById('pic4'));
			        	 option = {
		            		    title : {
		            		        text: '媒体覆盖对比',
		            		        subtext: '',
		            		        x:'center'
		            		    },
		            		    tooltip : {
		            		        trigger: 'item',
		            		        //小数和百分比
		            		        //formatter: "{a} <br/>{b} : {c}%"
		            		        //个数
		            		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		            		    },
		            		    legend: {
		            		        orient : 'vertical',
		            		        x : 'left',
		            		        data: legendArr
		            		    },
		            		    calculable : true,
		            		    series : [
		            		        {
		            		            name:'',
		            		            type:'pie',
		            		            radius : '55%',
		            		            center: ['50%', '60%'],
		            		            data: seriesArr
		            		        }
		            		    ]
		            	};
			        	 myChart.setOption(option);
			         }
			    );
			}
		}
	});
};

function requestSearchLanguage(word){
	var json = {
			"word":word,
			"starttime":"",
			"terminaltime":"",
			"language":""
		};
	
	hmProxy('search/searchLanguage', json, function(data, textStatus, jqXHR){
		data = eval("("+data+")");
		//测试数据，修改参考上面图
//		data = {
//			"ret": 0,
//			"msg": "success",
//			"data": [{
//				"language": "俄罗斯语",
//				"rate": "15"
//			}, {
//				"language": "日语",
//				"rate": "20"
//			}, {
//				"language": "韩语",
//				"rate": "18"
//			}, {
//				"language": "英语",
//				"rate": "600"
//			}, {
//				"language": "简体中文",
//				"rate": "318"
//			}, {
//				"language": "繁体中文",
//				"rate": "50"
//			}]
//		};
		if(data && data.ret == 0){
			var list = data.data;
			if(list && list.length > 0){
				var legendArr = new Array();
				var seriesArr = new Array();
				list.forEach(function(obj){
					legendArr.push(obj.language);
					var o = new Object();
					o.name = obj.language;
					o.value = obj.rate;
					seriesArr.push(o);
				});
				require(
			         [
			             'echarts',
			             'echarts/chart/pie'
			         ],
			         function (ec) {
			        	 var myChart = ec.init(document.getElementById('pic5'));
			        	 option = {
	            		    title : {
	            		        text: '多语言对比分析',
	            		        subtext: '',
	            		        x:'center'
	            		    },
	            		    tooltip : {
	            		        trigger: 'item',
	            		        formatter: "{a} <br/>{b} : {c} ({d}%)"
	            		    },
	            		    legend: {
	            		        orient : 'vertical',
	            		        x : 'left',
	            		        data:legendArr
	            		    },
	            		    calculable : true,
	            		    series : [
	            		        {
	            		            name:'',
	            		            type:'pie',
	            		            radius : '55%',
	            		            center: ['50%', '60%'],
	            		            clockWise:false,
	            		            data:seriesArr
	            		        }
	            		    ]
	            		}; 
			        	 myChart.setOption(option);
			         }
			   );
			}
		}
	});
};

function requestSide(id){
	var json = {
		"id":id
	};
	
	hmProxy('topic/side', json, function(data, textStatus, jqXHR){
//		data = eval("("+data+")");
		//测试数据，修改参考上面图
		data = {
			"ret": 0,
			"msg": "success",
			"data": {
				"positive": "82",
				"positiverate": "82%",
				"negative": "18",
				"negativerate": "18%"
			}
		};
		if(data && data.ret == 0){
			var data = data.data;
			if(data){
				var names = new Array();
				var datas = new Array();
				names.push({"value":data.positive, "name" : ("Positive "+data.positiverate)});
				names.push({"value":data.negative, "name" : ("Negative "+data.negativerate)});
				datas.push({"value":data.positive, "name" : "Positive"});
				datas.push({"value":data.negative, "name" : "Negative"});
				 require(
                         [
                             'echarts',
                             'echarts/chart/pie'
                         ],
                         function (ec) {
                             var myChart = ec.init(document.getElementById('pic6'));
                             var option = {
                        		 title : {
                                     text: '资讯正负面分布图',
                                     x:'120',
                                     y:'20',
                                     subtext: ''
                                 },	 
                                 tooltip : {
                                     trigger: 'item',
                                     formatter: "{a} <br/>{b} : {c} ({d}%)"
                                 },
                                 calculable : false,
                                 series : [

                                     {
                                         name:'',
                                         type:'pie',
                                         selectedMode: 'single',
                                         radius : [0, 90],
                                         x: '20%',
                                         width: '40%',
                                         funnelAlign: 'right',
                                         max: 1548,
                                         itemStyle : {
                                             normal : {
                                                 label : {
                                                     position : 'inner'
                                                 },
                                                 labelLine : {
                                                     show : false
                                                 }
                                             }
                                         },
                                         data:names
                                     },
                                     {
                                         name:'',
                                         type:'pie',
                                         clockWise:true,
                                         radius : [100, 140],
                                         x: '60%',
                                         width: '35%',
                                         funnelAlign: 'left',
                                         max: 1048,
                                         data:datas
                                     }
                                 ]
                             };
                             myChart.setOption(option);
                         }
                 );
			}
		}
	});
};